package control;

import java.util.ArrayList;
import server.mysqlConnection;


/**
 * @author Limor
 *This class responsible for the operation extend borrowed books, when the client wants to activate this 
 *operation he chooses book for extension in the GUI client. The book information inserted into string array that will send to the server. 
 * In the server we maps the message by checking the first string (see AbstractControl).
 */
public class ExtendBorrowControl extends AbstractControl {
	private String bID ;  
	private String bNAME ;
	private int flagPopular ;

	@Override
	public Object anlyze(String[] msg) {
		if(msg[1].contentEquals("WantToExtend"))
		{
			ArrayList<Object> result =new ArrayList<Object>();
			result.add("BorrowedBooks");
			String[][] borrows=null;
			String query=new String("SELECT name, borrowDate, borrow.expectedReturnDate FROM borrow, books WHERE books.bid=borrow.bId AND books.copy=borrow.copy AND returnDate='00/00/00' AND sId="+msg[2]+";");
			borrows=mysqlConnection.select(query,3);
			result.add(borrows);
			return result;	
		}
		else // check if it is possible to extend 
		{
			ArrayList<Object> result =new ArrayList<Object>();
			bNAME=msg[1];
			if(checkExist() && notPopular())
			{	
				String returnD=msg[4];
				String query=new String("UPDATE test.borrow SET expectedReturnDate="+returnD+" WHERE sid="+msg[2]+" AND borrowDate="+"'"+msg[3]+"'"+" AND bid="+bID+" AND returnDate='00/00/00'"+";");
				mysqlConnection.update(query);  
				String ansAfterUpdate=new String("SELECT name, borrowDate, borrow.expectedReturnDate FROM borrow, books WHERE books.bid=borrow.bId AND books.copy=borrow.copy AND sId="+msg[2]+" AND returnDate='00/00/00'"+";");
				String[][] ans=mysqlConnection.select(ansAfterUpdate,3);
				
				/// inform the librarian about the extension
				String log=new String("Extension book:"+bNAME+" for subscriber id: "+msg[2]+ " name:"+msg[5]); 
				logActivity("0",log);
				
				result.add("ExtendBooks");
				result.add(ans);
				return result;
					
						
			}
			else 
			{
				result.add("NotExtend");
				if (flagPopular==1) result.add("Popular book can not be extended");
				else result.add("All copies are borrowed and there is order for this book - can't extend");
				return result; 
			}

		} 
	}


	@Override
	public Boolean checkExist()
	{
		String bookBid=new String("SELECT bid FROM books WHERE name="+"'"+bNAME+"'"+";"); 
		bID=mysqlConnection.check(bookBid);
		
		String query=new String("SELECT bid FROM books WHERE bid="+bID+" AND isborrowed=0;"); 
		String borrowed=mysqlConnection.check(query);
		if(borrowed.contentEquals("NotEXIST"))   
		{
			String query2=new String("SELECT bid FROM orders WHERE bid="+bID+";");
			String hasOrders=mysqlConnection.check(query2);
			if(hasOrders.contentEquals("NotEXIST")) return true;   // all copies are borrowed but there isn't order for this book 
			else return false; 
		}	
		else return true;   // it means that there are available copies 
	}

	
	
	/**
	 * This function checks if book is popular 
	 * @return true if the book that was chosen for extension is not popular 
	 */
	public Boolean notPopular()
	{
		String query=new String("SELECT bid FROM books WHERE name="+"'"+bNAME+"'"+" AND ispopular=0"+";");
		String ans=mysqlConnection.check(query);
		if(ans.contentEquals("NotEXIST")) 
		{
			flagPopular=1; 
			return false;
		}
		return true;
	}
	
}